(function() {
	var login = {
		init: function() {
			var that = this;
			if (that.setting.loginApi) {
				this.$form = $("<div>", {
					"class": "form-horizontal"
				})
			} else {
				this.$form = $("<form>", {
					"class": "form-horizontal",
					"method": "post",
					"action": that.setting.loginUrl
				})
			}
			this.$form.appendTo(that.container)
				.append(that.createUser())
				.append(that.createPass())
				.append(that.createCode())
				.append($("<div>", {
					"class": "row mb15 text-center"
				}).append(that.createLoginBtn()).append(that.createRegBtn()))
				.append(that.createForgetBtn())
			that.events();
		},
		events: function() {
			var that = this;
			var sucGlyphicon = '<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>';
			var errGlyphicon = '<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>';

			that.$form.find(".has-feedback").each(function() {
				//$(this).append(sucGlyphicon);
				$(this).append(errGlyphicon);
			});

			$([that.$userInput[0], that.$passInput[0]]).each(function() {
				$(this).on("blur", function() {
					if ($.trim($(this).val())) {
						$(this).parent().removeClass("has-error").addClass("has-success")
					} else {
						$(this).parent().removeClass("has-success").addClass("has-error")
					}
				})
			});
			if (that.$codeImg) {
				that.$codeImg.on('click', function() {
					$(this).attr('src', that.setting.codeImgUrl + '?' + Math.random());
				});
			};
			if (that.$codeInput) {
				that.$codeInput.on("blur", function() {
					var $this = $(this),
						$div = $(this).parent();
					if ($.trim($this.val()) && that.setting.verifyCodeApi) {
						var formData = {};
						formData[that["setting"]["codeField"]] = $.trim($(this).val())
						$.ajax({
							url: that.setting.verifyCodeApi,
							data: formData,
							async: false,
							suc: function(data) {
								$div.addClass("has-success").removeClass("has-error");
							},
							err: function() {
								$div.removeClass("has-success").addClass("has-error");
							}
						});
					} else if (!$.trim($this.val())) {
						$div.removeClass("has-success").addClass("has-error");
					}
				}).on("focus", function() {
					$div = $(this).parent();
					$div.removeClass("has-success").removeClass("has-error")
				})
			}
			that.$loginBtn.on("click", function() {
				that.$form.find(".form-control").trigger("blur");
				if (that.$form.find(".has-error").length > 0) {
					return false;
				}
				if (that.setting.loginUrl) {
					that.$form[0].submit();
				} else if (that.setting.loginApi) {
					$.ajax({
						type: "post",
						url: that.setting.loginApi,
						data: that.$form.sendForm(),
						suc: function(data) {
							window.location.href = that.setting.homePageUrl;
						},
						hasAlert: true,
						loadingContainer: $("body")
					});
				}
				return false;
			});
			that.$form.on("keyup", "input.form-control", function(e) {
				if (e.which == 13) {
					that.$loginBtn.trigger("click")
				}
			})
		},
		createUser: function() {
			var that = this;
			if (that.setting.userLabelText) {
				var $div = $("<div>", {
					"class": "form-group"
				})
				that.createLabel(that.setting.userLabelText).appendTo($div);
				this.$userInput = that.createInput(that.setting.userField, "text", that.setting.namePlaceholder).appendTo(that.createDiv().appendTo($div))
			} else {
				var $div = $("<div>", {
					"class": "input-group mb15"
				})
				$("<span>", {
					"class": "input-group-addon"
				}).append($("<span>", {
					"class": "glyphicon glyphicon-user"
				})).appendTo($div);
				this.$userInput = that.createInput(that.setting.userField, "text", that.setting.namePlaceholder).appendTo($div.addClass("has-feedback"))
			}
			return $div;
		},
		createPass: function() {
			var that = this;
			if (that.setting.passLabelText) {
				var $div = $("<div>", {
					"class": "form-group"
				})
				that.createLabel(that.setting.passLabelText).appendTo($div);
				this.$passInput = that.createInput(that.setting.passField, "password", that.setting.passPlaceholder).appendTo(that.createDiv().appendTo($div))
			} else {
				var $div = $("<div>", {
					"class": "input-group mb15"
				});
				$("<span>", {
					"class": "input-group-addon"
				}).append($("<span>", {
					"class": "glyphicon glyphicon-lock"
				})).appendTo($div);
				this.$passInput = that.createInput(that.setting.passField, "password", that.setting.passPlaceholder).appendTo($div.addClass("has-feedback"))
			}
			return $div;
		},
		createCode: function() {
			var that = this;
			if (that.setting.codeImgUrl) {
				this.$code = $("<div>", {
					"class": "form-group"
				})
				this.$codeInput = that.createInput(that.setting.codeField, "text", that.setting.codePlaceholder)
				this.$codeImg = $("<img>", {
					"class": "img-responsive pointer",
					"style": "max-height:34px;",
					"src": that.setting.codeImgUrl
				})
				if (that.setting.codeLabelText) {
					that.createLabel(that.setting.codeLabelText).appendTo(this.$code)
					var $row = $("<div>", {
						"class": "row"
					}).appendTo(that.createDiv().appendTo(this.$code))
					this.$codeInput.appendTo(that.createDiv(5).appendTo($row))
					this.$codeImg.appendTo(that.createDiv(5).addClass("pl5").addClass("pr5").appendTo($row))

				} else {
					var $row = $("<div>", {
						"class": "row"
					}).appendTo(this.$code);
					this.$codeInput.appendTo(that.createDiv(5).appendTo(this.$code));
					this.$codeImg.appendTo(that.createDiv(4).addClass("pl5").addClass("pr5").appendTo(this.$code));
				}
				return this.$code;
			} else {
				return "";
			}
		},
		createLoginBtn: function() {
			var that = this;
			this.$loginBtn = $("<button>", {
				"class": "btn btn-primary btn-block"
			}).html(this.setting.loginBtnText)
			if (this.setting.btnAlign == "center" || this.setting.btnAlign == "right") {
				var n = parseInt(this.setting.btnCols),
					offset = 0;
				n += this.setting.regUrl ? parseInt(this.setting.btnCols) : 0;
				if (this.setting.btnAlign == "center") {
					offset = parseInt((12 - n) / 2);
				} else if (this.setting.btnAlign == "right") {
					offset = 12 - n;
				}
				return this.createBtnContainer().addClass("col-xs-offset-" + offset).append(this.$loginBtn)
			} else {
				return this.createBtnContainer().append(this.$loginBtn)
			}

		},
		createRegBtn: function() {
			var that = this;
			if (this.setting.regUrl) {
				this.$regBtn = $("<button>", {
					"class": "btn btn-info btn-block"
				}).html(this.setting.regBtnText).on("click", function() {
					window.location.href = that.setting.regUrl;
				});
				return this.createBtnContainer().append(this.$regBtn)
			} else {
				return ""
			}
		},
		createForgetBtn: function() {
			if (this.setting.forgetUrl) {
				return $("<div>", {
					"class": "text-right pr15"
				}).append($("<a>", {
					"href": this.setting.forgetUrl
				}).html(this.setting.forgetBtnText))
			} else {
				return ""
			}
		},
		createLabel: function(text) {
			return $("<label>", {
				"class": "label-control col-xs-" + this.setting.labelCols
			}).html(text)
		},
		createDiv: function(cols) {
			return $("<div>", {
				"class": "col-xs-" + (cols || (12 - parseInt(this.setting.labelCols))) + " has-feedback"
			})
		},
		createInput: function(name, type, placeholder) {
			return $("<input>", {
				"class": "form-control",
				"name": name,
				"type": type,
				"required": "required",
				"placeholder": placeholder
			})
		},
		createBtnContainer: function() {
			return $("<div>", {
				"class": "col-xs-" + this.setting.btnCols + " mb15"
			})
		}
	};
	var loginForm = function() {
		this.login = $.extend({}, login);
	}
	jQuery.fn.extend({
		loginForm: function(options) {
			login.container = $(this);
			login.setting = options = $.extend({
				labelCols: 2, //标题所占栅格
				userLabelText: "", //用户名标题
				passLabelText: "", //密码标题
				codeLabelText: "", //验证码标题
				userField: "username", //用户名字段
				passField: "password", //密码字段
				codeField: "code", //验证码字段
				codeImgUrl: "", //验证码图片地址
				verifyCodeApi: "", //验证验证码接口
				verifyUserApi: "", //验证用户名是否正常接口，非正常需要验证码
				namePlaceholder: "用户名", //用户名输入框提示
				passPlaceholder: "密码", //密码输入框提示
				codePlaceholder: "验证码", //验证码输入框提示
				loginUrl: "", //表单提交方式提交地址
				loginApi: "", //接口提交方式接口地址
				homePageUrl: "/index.html", //接口提交方式，登录成功后跳转地址
				loginBtnText: "登录", //登录按钮文字
				regBtnText: "注册", //是否有注册按钮
				regUrl: "", //注册跳转地址
				forgetBtnText: "忘记密码", //忘记密码按钮文字
				forgetUrl: "", //忘记密码跳转地址,
				btnCols: 12, //按钮宽度栅格
				btnAlign: "center"
			}, options)
			var l = new loginForm;
			l.login.init();
			return l;
		}

	})
})();
